package com.neusoft.bizcore.notice.notification.controller;

import java.util.concurrent.ExecutorService;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.notice.notification.dto.NoticeRecordDTO;
import com.neusoft.bizcore.notice.notification.service.NoticeService;
import com.neusoft.bizcore.notice.system.service.SystemNoticeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags = "创建通知")
@RestController
@Slf4j
@RequestMapping(value = "/api/w/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    //    @Autowired(required = false)
    //    private AsyncCallService callService;

    @Resource(name = "noticeDataExecutor")
    ExecutorService noticeDataExecutor;

    @Autowired
    private SystemNoticeService SystemNoticeService;

    @ApiOperation("创建通知")
    @PostMapping
    public void create(@RequestBody final NoticeRecordDTO dto) {
        NoticeController.log.info("接收到系统通知----开始");
        //        final Callable<String> callable = new Callable<String>() {
        //            @Override
        //            public String call() throws Exception {
        //                NoticeController.this.noticeService.buildAndSendMessage(dto);
        //                return null;
        //            }
        //        };
        //        //使用service创建的线程池，处理controller请求产生的一个个的任务
        //        this.callService.handle(callable);
        final SendMessageRunnable runnable = new SendMessageRunnable(dto);
        this.noticeDataExecutor.execute(runnable);
        NoticeController.log.info("接收到系统通知---结束");
    }

    @ApiOperation("通知Page")
    @PostMapping("/refresh/{path}")
    public void notifyPage(@PathVariable final String path, @RequestParam final String message) {
        NoticeController.log.info("通知Page----开始");
        //        final Callable<String> callable = new Callable<String>() {
        //            @Override
        //            public String call() throws Exception {
        //                NoticeController.this.SystemNoticeService.notifyPage(path, message);
        //                return null;
        //            }
        //        };
        //        this.callService.handle(callable);
        final NotifyPageRunnable runnable = new NotifyPageRunnable(path, message);
        this.noticeDataExecutor.execute(runnable);
        NoticeController.log.info("通知Page---结束");
    }

    class SendMessageRunnable implements Runnable {

        NoticeRecordDTO dto;

        public SendMessageRunnable(final NoticeRecordDTO dto) {
            this.dto = dto;
        }

        @Override
        public void run() {
            NoticeController.this.noticeService.buildAndSendMessage(this.dto);

        }
    }

    class NotifyPageRunnable implements Runnable {

        String path;
        String message;

        NotifyPageRunnable(final String path, final String message) {
            this.path = path;
            this.message = message;
        }

        @Override
        public void run() {
            NoticeController.this.SystemNoticeService.notifyPage(this.path, this.message);
        }

    }
}
